package com.blog.utils;

import io.jsonwebtoken.*;
import org.junit.Test;

import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Date;

public class JwtUtil {
    private static final long expire=24*60*60*1000L;
    private static final String key="all";

    public static String encode(String username){
        String token = Jwts.builder().setExpiration(new Date(System.currentTimeMillis() + expire))
                .setSubject(username)
               .signWith(SignatureAlgorithm.HS512,Base64.getEncoder().encode(key.getBytes(StandardCharsets.UTF_8)))
                .compact();
        return token;
    }

    public static Claims decode(String token){
        Jws<Claims> jws = Jwts.parser()
                .setSigningKey(Base64.getEncoder().encode(key.getBytes(StandardCharsets.UTF_8)))
                .parseClaimsJws(token);
        Claims claims = jws.getBody();
        return claims;

    }

}
